import {Component, OnInit, ViewChild} from '@angular/core';
import {FormControl, FormGroup} from "@angular/forms";
import {UserInfoService} from "../service/user-info.service";
import {BarnUser} from "../entity/barn-user";
import {environment} from "../../../../../../environments/environment";


@Component({
  selector: 'app-user-info',
  templateUrl: './user-info.component.html',
  styleUrls: ['./user-info.component.less']
})
export class UserInfoComponent implements OnInit{

  validateForm = new FormGroup({
    userName: new FormControl(),
    userAccount: new FormControl(),
    enable: new FormControl('')
  })

  users: BarnUser[]
  current = 1;
  pageSize = 20;
  total = 1000;
  pageSizes = [this.pageSize, 50, 100]
  loading = false

  @ViewChild('tableElement', {static: true}) table

  constructor(private userInfoService: UserInfoService) {

  }


  ngOnInit(): void {
    this.search();
  }

  resetForm() {
    this.validateForm.reset();
  }

  search() {
    const param = this.validateForm.getRawValue();
    this.loading = true
    this.userInfoService.queryUser(param, this.current, this.pageSize).subscribe(value => {
      this.users = value.data
      this.current = value[environment.page.current] || 1
      this.total = value[environment.page.count] || 1
    }, () => this.loading = false, () => this.loading = false);
  }
}
